﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public class UserSetting
	{
		public UserSetting()
		{
			this.filePath = Path.Combine(Product.UserDataLocation, "HYMarkSetting.xml");
			if (!File.Exists(this.filePath))
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYMarkSetting.xml"), this.filePath);
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.filePath);
			this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
			if ((this.xmlNodeRoot as XmlElement).GetAttribute("version") != "BIMSpace2019")
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYMarkSetting.xml"), this.filePath, true);
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(this.filePath);
				this.xmlNodeRoot = this.xmlDoc.FirstChild.NextSibling;
			}
		}

		public CommonFont GetFontSetting()
		{
			CommonFont commonFont = new CommonFont();
			string parentNode = "CommonFont";
			commonFont.Font = this.GetValue(parentNode, "Font", "仿宋");
			commonFont.FontHeight = this.GetDoubleValue(this.GetValue(parentNode, "FontHeight", "3"));
			commonFont.FontGWRotia = this.GetDoubleValue(this.GetValue(parentNode, "FontGWRotia", "0.7"));
			commonFont.Align = this.GetValue(parentNode, "Align", "中心对齐");
			commonFont.LeadRotia = this.GetDoubleValue(this.GetValue(parentNode, "LeadRotia", "1.1"));
			commonFont.Presice = this.GetValue(parentNode, "Presice", "0.000");
			commonFont.LeadDistance = this.GetDoubleValue(this.GetValue(parentNode, "LeadDistance", "2"));
			commonFont.TubeDistance = this.GetDoubleValue(this.GetValue(parentNode, "TubeDistance", "2"));
			return commonFont;
		}

		public void WriteFontSetting(CommonFont commonFont)
		{
			if (commonFont == null)
			{
				return;
			}
			string parentNode = "CommonFont";
			this.WriteValue(parentNode, "Font", commonFont.Font);
			this.WriteValue(parentNode, "FontHeight", commonFont.FontHeight.ToString());
			this.WriteValue(parentNode, "FontGWRotia", commonFont.FontGWRotia.ToString());
			this.WriteValue(parentNode, "Align", commonFont.Align);
			this.WriteValue(parentNode, "LeadDistance", commonFont.LeadDistance.ToString());
			this.WriteValue(parentNode, "Presice", commonFont.Presice);
			this.WriteValue(parentNode, "LeadRotia", commonFont.LeadRotia.ToString());
			this.WriteValue(parentNode, "TubeDistance", commonFont.TubeDistance.ToString());
		}

		public TubeSynthesize GetTubeSynthesize()
		{
			TubeSynthesize tubeSynthesize = new TubeSynthesize();
			string parentNode = "TubeSynthesize";
			tubeSynthesize.Pipe = this.GetBoolValue(parentNode, "Pipe", true);
			tubeSynthesize.Duct = this.GetBoolValue(parentNode, "Duct", false);
			tubeSynthesize.CableTray = this.GetBoolValue(parentNode, "CableTray", false);
			tubeSynthesize.Conduit = this.GetBoolValue(parentNode, "Conduit", true);
			tubeSynthesize.BeOffset = this.GetBoolValue(parentNode, "BeOffset", true);
			return tubeSynthesize;
		}

		public void WriteTubeSynthesize(TubeSynthesize synthesize)
		{
			if (synthesize == null)
			{
				return;
			}
			string parentNode = "TubeSynthesize";
			this.WriteValue(parentNode, "BeOffset", synthesize.BeOffset.ToString());
			this.WriteValue(parentNode, "Pipe", synthesize.Pipe.ToString());
			this.WriteValue(parentNode, "Duct", synthesize.Duct.ToString());
			this.WriteValue(parentNode, "CableTray", synthesize.CableTray.ToString());
			this.WriteValue(parentNode, "Conduit", synthesize.Conduit.ToString());
		}

		public QuickPosition.QuickType GetQuickPositionType()
		{
			string value = this.GetValue("QuickPosition", "Ruler", "Proximity");
			return (QuickPosition.QuickType)Enum.Parse(typeof(QuickPosition.QuickType), value);
		}

		public void WriteQuickPositionType(QuickPosition.QuickType quickType)
		{
			this.WriteValue("QuickPosition", "Ruler", quickType.ToString());
		}

		public DuctMarkSet GetDuctMarkSetting()
		{
			DuctMarkSet ductMarkSet = new DuctMarkSet();
			string parentNode = "DuctMark";
			ductMarkSet.MarkContent = this.GetValue(parentNode, "MarkContent", "相对标高");
			ductMarkSet.OffsetPrefix = this.GetValue(parentNode, "OffsetPrefix", "H");
			ductMarkSet.OffsetSuffix = this.GetValue(parentNode, "OffsetSuffix", "m");
			ductMarkSet.Splite = this.GetValue(parentNode, "Splite", ",");
			ductMarkSet.TubeLength = this.GetDoubleValue(this.GetValue(parentNode, "TubeLength", "1000"));
			ductMarkSet.DuctSuffix = this.GetValue(parentNode, "DuctSuffix", "无");
			ductMarkSet.DuctPrefix = this.GetValue(parentNode, "DuctPrefix", "Ф");
			ductMarkSet.UnitVisible = this.GetValue(parentNode, "Visible", "不显示");
			ductMarkSet.PrefixSource = this.GetValue(parentNode, "PrefixSource", "底$中$顶$无$H$h$F$f$A$a");
			return ductMarkSet;
		}

		public List<string> GetTagNameList()
		{
			List<string> list = new List<string>();
			foreach (object obj in this.xmlNodeRoot.SelectSingleNode("TagNameList").ChildNodes)
			{
				XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
				list.Add(xmlElement.InnerText);
			}
			return list;
		}

		public void WriteDuctMarkSetting(DuctMarkSet markSet)
		{
			if (markSet == null)
			{
				return;
			}
			string parentNode = "DuctMark";
			this.WriteValue(parentNode, "DuctPrefix", markSet.DuctPrefix);
			this.WriteValue(parentNode, "DuctSuffix", markSet.DuctSuffix);
			this.WriteValue(parentNode, "MarkContent", markSet.MarkContent);
			this.WriteValue(parentNode, "OffsetPrefix", markSet.OffsetPrefix);
			this.WriteValue(parentNode, "OffsetSuffix", markSet.OffsetSuffix);
			this.WriteValue(parentNode, "Splite", markSet.Splite);
			this.WriteValue(parentNode, "TubeLength", markSet.TubeLength.ToString());
			this.WriteValue(parentNode, "Visible", markSet.UnitVisible.ToString());
			this.WriteValue(parentNode, "PrefixSource", markSet.PrefixSource);
		}

		public TerminalMarkSet GetTerminalMarkSetting()
		{
			TerminalMarkSet terminalMarkSet = new TerminalMarkSet();
			string parentNode = "TerminalMark";
			terminalMarkSet.DrawLineBox = this.GetValue(parentNode, "DrawLineBox", bool.TrueString);
			terminalMarkSet.Splite = this.GetValue(parentNode, "Splite", "空格");
			return terminalMarkSet;
		}

		public void WriteTerminalMarkSetting(TerminalMarkSet markSet)
		{
			if (markSet == null)
			{
				return;
			}
			string parentNode = "TerminalMark";
			this.WriteValue(parentNode, "DrawLineBox", markSet.DrawLineBox);
			this.WriteValue(parentNode, "Splite", markSet.Splite);
		}

		public PipeDiameterMarkSet GetDiameterMarkSetting()
		{
			PipeDiameterMarkSet pipeDiameterMarkSet = new PipeDiameterMarkSet();
			string parentNode = "DiameterMark";
			pipeDiameterMarkSet.Diameter = this.GetValue(parentNode, "Diameter", "");
			pipeDiameterMarkSet.DrawSlash = this.GetValue(parentNode, "DrawSlash", bool.TrueString);
			pipeDiameterMarkSet.PipeOffset = this.GetValue(parentNode, "PipeOffset", bool.FalseString);
			pipeDiameterMarkSet.Reduce = this.GetValue(parentNode, "Reduce", bool.FalseString);
			pipeDiameterMarkSet.Splite = this.GetValue(parentNode, "Splite", "X");
			pipeDiameterMarkSet.SystemType = this.GetValue(parentNode, "SystemType", bool.FalseString);
			pipeDiameterMarkSet.Thickness = this.GetValue(parentNode, "Thickness", bool.FalseString);
			pipeDiameterMarkSet.TubeDiameter = this.GetDoubleValue(this.GetValue(parentNode, "TubeDiameter", "15"));
			pipeDiameterMarkSet.TubeLength = this.GetDoubleValue(this.GetValue(parentNode, "TubeLength", "3000"));
			pipeDiameterMarkSet.TwoSides = this.GetValue(parentNode, "TwoSides", bool.FalseString);
			return pipeDiameterMarkSet;
		}

		public void WriteDiameterMarkSetting(PipeDiameterMarkSet markSet)
		{
			if (markSet == null)
			{
				return;
			}
			string parentNode = "DiameterMark";
			this.WriteValue(parentNode, "Diameter", markSet.Diameter);
			this.WriteValue(parentNode, "DrawSlash", markSet.DrawSlash);
			this.WriteValue(parentNode, "PipeOffset", markSet.PipeOffset);
			this.WriteValue(parentNode, "Reduce", markSet.Reduce);
			this.WriteValue(parentNode, "Splite", markSet.Splite);
			this.WriteValue(parentNode, "SystemType", markSet.SystemType);
			this.WriteValue(parentNode, "Thickness", markSet.Thickness);
			this.WriteValue(parentNode, "TubeDiameter", markSet.TubeDiameter.ToString());
			this.WriteValue(parentNode, "TubeLength", markSet.TubeLength.ToString());
			this.WriteValue(parentNode, "TwoSides", markSet.TwoSides);
		}

		public PipeOffsetMarkSet GetOffsetMarkSetting()
		{
			PipeOffsetMarkSet pipeOffsetMarkSet = new PipeOffsetMarkSet();
			string parentNode = "OffsetMark";
			pipeOffsetMarkSet.Content = this.GetValue(parentNode, "Content", "相对标高");
			pipeOffsetMarkSet.Prefix = this.GetValue(parentNode, "Prefix", "H");
			pipeOffsetMarkSet.Splite = this.GetValue(parentNode, "Splite", ",");
			pipeOffsetMarkSet.Suffix = this.GetValue(parentNode, "Suffix", "m");
			pipeOffsetMarkSet.UnitVisible = this.GetValue(parentNode, "Visible", "不显示");
			pipeOffsetMarkSet.PrefixSource = this.GetValue(parentNode, "PrefixSource", "H$h$F$f$A$a$无");
			return pipeOffsetMarkSet;
		}

		public void WriteOffsetMarkSetting(PipeOffsetMarkSet markSet)
		{
			if (markSet == null)
			{
				return;
			}
			string parentNode = "OffsetMark";
			this.WriteValue(parentNode, "Content", markSet.Content);
			this.WriteValue(parentNode, "Prefix", markSet.Prefix);
			this.WriteValue(parentNode, "Splite", markSet.Splite);
			this.WriteValue(parentNode, "Suffix", markSet.Suffix);
			this.WriteValue(parentNode, "Visible", markSet.UnitVisible);
			this.WriteValue(parentNode, "PrefixSource", markSet.PrefixSource);
		}

		public SlopeMarkSet GetSlopeMarkSetting()
		{
			SlopeMarkSet slopeMarkSet = new SlopeMarkSet();
			string parentNode = "SlopeMark";
			slopeMarkSet.SlopeStyle = this.GetValue(parentNode, "SlopeStyle", "0.001");
			slopeMarkSet.Style = this.GetValue(parentNode, "Style", "全箭头");
			return slopeMarkSet;
		}

		public void WriteSlopeMarkSetting(SlopeMarkSet markSet)
		{
			if (markSet == null)
			{
				return;
			}
			string parentNode = "SlopeMark";
			this.WriteValue(parentNode, "SlopeStyle", markSet.SlopeStyle);
			this.WriteValue(parentNode, "Style", markSet.Style);
		}

		public void WriteDuctIndependentSetting(DuctIndependentData Independent)
		{
			if (Independent == null)
			{
				return;
			}
			string parentNode = "DuctIndependent";
			this.WriteValue(parentNode, "IsAir", Independent.IsAir.ToString());
			this.WriteValue(parentNode, "IsCode", Independent.IsCode.ToString());
			this.WriteValue(parentNode, "IsLevel", Independent.IsLevel.ToString());
			this.WriteValue(parentNode, "IsLine", Independent.IsLine.ToString());
			this.WriteValue(parentNode, "IsSingle", Independent.IsSingle.ToString());
			this.WriteValue(parentNode, "IsSize", Independent.IsSize.ToString());
			this.WriteValue(parentNode, "IsSpeed", Independent.IsSpeed.ToString());
			this.WriteValue(parentNode, "StrLevel", Independent.StrLevel.ToString());
			this.WriteValue(parentNode, "StrLine", Independent.StrLine.ToString());
		}

		public DuctIndependentData ReadDuctIndependentSetting()
		{
			DuctIndependentData ductIndependentData = new DuctIndependentData();
			string parentNode = "DuctIndependent";
			ductIndependentData.StrLevel = this.GetValue(parentNode, "StrLevel", "");
			ductIndependentData.StrLine = this.GetValue(parentNode, "StrLine", "");
			ductIndependentData.IsAir = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsAir", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsAir", "")));
			ductIndependentData.IsCode = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsCode", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsCode", "")));
			ductIndependentData.IsLevel = (string.IsNullOrEmpty(this.GetValue(parentNode, "IsLevel", "")) || Convert.ToBoolean(this.GetValue(parentNode, "IsLevel", "")));
			ductIndependentData.IsLine = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsLine", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsLine", "")));
			ductIndependentData.IsSingle = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsSingle", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsSingle", "")));
			ductIndependentData.IsSize = (string.IsNullOrEmpty(this.GetValue(parentNode, "IsSize", "")) || Convert.ToBoolean(this.GetValue(parentNode, "IsSize", "")));
			ductIndependentData.IsSpeed = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsSpeed", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsSpeed", "")));
			return ductIndependentData;
		}

		public void WriteSlopeIndependentSetting(SlopeIndependentData Independent)
		{
			if (Independent == null)
			{
				return;
			}
			string parentNode = "SlopeIndependent";
			this.WriteValue(parentNode, "Solpe", Independent.Solpe);
			this.WriteValue(parentNode, "IsStream", Independent.IsStream.ToString());
			this.WriteValue(parentNode, "IsOnline", Independent.IsOnline.ToString());
		}

		public SlopeIndependentData ReadSlopeIndependentSetting()
		{
			SlopeIndependentData slopeIndependentData = new SlopeIndependentData();
			string parentNode = "SlopeIndependent";
			slopeIndependentData.Solpe = this.GetValue(parentNode, "Solpe", "");
			slopeIndependentData.IsOnline = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsOnline", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsOnline", "")));
			slopeIndependentData.IsStream = (!string.IsNullOrEmpty(this.GetValue(parentNode, "IsStream", "")) && Convert.ToBoolean(this.GetValue(parentNode, "IsStream", "")));
			return slopeIndependentData;
		}

		public InHomePipeData GetInHomePipeSetting()
		{
			InHomePipeData inHomePipeData = new InHomePipeData();
			try
			{
				string parentNode = "InHomePipe";
				string value = this.GetValue(parentNode, "DimStype", "");
				if (!string.IsNullOrEmpty(value))
				{
					inHomePipeData.DimStype = int.Parse(value);
				}
				value = this.GetValue(parentNode, "DimContent", "");
				if (!string.IsNullOrEmpty(value))
				{
					inHomePipeData.DimContent = int.Parse(value);
				}
				value = this.GetValue(parentNode, "Content", "");
				if (!string.IsNullOrEmpty(value))
				{
					inHomePipeData.Content = value;
				}
				value = this.GetValue(parentNode, "IsLead", "");
				if (!string.IsNullOrEmpty(value))
				{
					if (value.ToLower() == "true")
					{
						inHomePipeData.IsLead = true;
					}
					else
					{
						inHomePipeData.IsLead = false;
					}
				}
			}
			catch
			{
			}
			return inHomePipeData;
		}

		public void WriteInHomePipeSetting(InHomePipeData markSet)
		{
			string parentNode = "InHomePipe";
			this.WriteValue(parentNode, "DimStype", markSet.DimStype.ToString());
			this.WriteValue(parentNode, "DimContent", markSet.DimContent.ToString());
			this.WriteValue(parentNode, "Content", markSet.Content);
			this.WriteValue(parentNode, "IsLead", markSet.IsLead.ToString());
		}

		public DimEquipmentData GetDimEquipmentSetting()
		{
			DimEquipmentData dimEquipmentData = new DimEquipmentData();
			try
			{
				string parentNode = "DimEquipment";
				string value = this.GetValue(parentNode, "DimStype", "");
				if (!string.IsNullOrEmpty(value))
				{
					dimEquipmentData.DimStype = int.Parse(value);
				}
				value = this.GetValue(parentNode, "FloorName", "");
				if (!string.IsNullOrEmpty(value))
				{
					dimEquipmentData.FloorName = value;
				}
				value = this.GetValue(parentNode, "DimContent", "");
				if (!string.IsNullOrEmpty(value))
				{
					dimEquipmentData.DimContent = value;
				}
				value = this.GetValue(parentNode, "NumberType", "");
				if (!string.IsNullOrEmpty(value))
				{
					dimEquipmentData.NumberType = int.Parse(value);
				}
				value = this.GetValue(parentNode, "NumberContent", "");
				if (!string.IsNullOrEmpty(value))
				{
					dimEquipmentData.NumberContent = value;
				}
				value = this.GetValue(parentNode, "IsLead", "");
				if (!string.IsNullOrEmpty(value))
				{
					if (value.ToLower() == "true")
					{
						dimEquipmentData.IsLead = true;
					}
					else
					{
						dimEquipmentData.IsLead = false;
					}
				}
			}
			catch
			{
			}
			return dimEquipmentData;
		}

		public void WriteDimEquipmentSetting(DimEquipmentData markSet)
		{
			string parentNode = "DimEquipment";
			this.WriteValue(parentNode, "DimStype", markSet.DimStype.ToString());
			this.WriteValue(parentNode, "FloorName", markSet.FloorName);
			this.WriteValue(parentNode, "DimContent", markSet.DimContent.ToString());
			this.WriteValue(parentNode, "NumberType", markSet.NumberType.ToString());
			this.WriteValue(parentNode, "NumberContent", markSet.NumberContent);
			this.WriteValue(parentNode, "IsLead", markSet.IsLead.ToString());
		}

		public List<PrefixSetting> GetAllPrefixSetting()
		{
			List<PrefixSetting> list = new List<PrefixSetting>();
			string xpath = "PrefixSetting";
			try
			{
				foreach (object obj in this.xmlNodeRoot.SelectSingleNode(xpath).ChildNodes)
				{
					XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
					list.Add(new PrefixSetting
					{
						MaterialName = xmlElement.GetAttribute("Material"),
						PrefixValue = xmlElement.GetAttribute("Prefix")
					});
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				list = null;
			}
			return list;
		}

		public void WriteAllPrefixSetting(List<PrefixSetting> listPrefix)
		{
			string xpath = "PrefixSetting";
			XmlNode xmlNode = this.xmlNodeRoot.SelectSingleNode(xpath);
			xmlNode.RemoveAll();
			foreach (PrefixSetting prefixSetting in listPrefix)
			{
				XmlElement xmlElement = this.xmlDoc.CreateElement("PrefixSet");
				xmlElement.SetAttribute("Material", prefixSetting.MaterialName);
				xmlElement.SetAttribute("Prefix", prefixSetting.PrefixValue);
				xmlNode.AppendChild(xmlElement);
			}
			this.xmlDoc.Save(this.filePath);
		}

		private double GetDoubleValue(string strValue)
		{
			double result;
			try
			{
				if (string.IsNullOrEmpty(strValue))
				{
					result = 0.0;
				}
				else
				{
					result = Convert.ToDouble(strValue);
				}
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private string GetValue(string parentNode, string subNode, string defaultValue = "")
		{
			string result;
			try
			{
				result = this.xmlNodeRoot.SelectSingleNode(parentNode).SelectSingleNode(subNode).InnerText;
			}
			catch (Exception)
			{
				result = defaultValue;
			}
			return result;
		}

		private bool GetBoolValue(string parentNode, string subNode, bool defaultValue = false)
		{
			bool result;
			try
			{
				result = Convert.ToBoolean(this.xmlNodeRoot.SelectSingleNode(parentNode).SelectSingleNode(subNode).InnerText);
			}
			catch (Exception)
			{
				result = defaultValue;
			}
			return result;
		}

		private void WriteValue(string parentNode, string subNode, string text)
		{
			try
			{
				XmlNode xmlNode = this.xmlNodeRoot.SelectSingleNode(parentNode);
				if (xmlNode == null)
				{
					xmlNode = this.xmlDoc.CreateElement(parentNode);
					this.xmlNodeRoot.AppendChild(xmlNode);
				}
				XmlNode xmlNode2 = xmlNode.SelectSingleNode(subNode);
				if (xmlNode2 != null)
				{
					xmlNode2.InnerText = text;
				}
				else
				{
					XmlNode xmlNode3 = this.xmlDoc.CreateElement(subNode);
					xmlNode3.InnerText = text;
					xmlNode.AppendChild(xmlNode3);
				}
				this.xmlDoc.Save(this.filePath);
			}
			catch (Exception)
			{
			}
		}

		public void WriteLevIndTagData(string[] data)
		{
			XMLOperating.EditNodeText(this.filePath, "LevelIndependentTag", "AnnType", data[0]);
			XMLOperating.EditNodeText(this.filePath, "LevelIndependentTag", "AnnMode", data[1]);
			XMLOperating.EditNodeText(this.filePath, "LevelIndependentTag", "AnnStyle", data[2]);
			XMLOperating.EditNodeText(this.filePath, "LevelIndependentTag", "AnnLocation", data[3]);
		}

		public string[] ReadLevIndTagData()
		{
			return new string[]
			{
				XMLOperating.GetNodeText(this.xmlDoc, "LevelIndependentTag", "AnnType"),
				XMLOperating.GetNodeText(this.xmlDoc, "LevelIndependentTag", "AnnMode"),
				XMLOperating.GetNodeText(this.xmlDoc, "LevelIndependentTag", "AnnStyle"),
				XMLOperating.GetNodeText(this.xmlDoc, "LevelIndependentTag", "AnnLocation")
			};
		}

		public void WriteDuctTerminalTagData(string[] data)
		{
			XMLOperating.EditNodeText(this.filePath, "DuctTerminal", "Name", data[0]);
			XMLOperating.EditNodeText(this.filePath, "DuctTerminal", "AirAmount", data[1]);
		}

		public string[] ReadDuctTerminalTagData()
		{
			return new string[]
			{
				XMLOperating.GetNodeText(this.xmlDoc, "DuctTerminal", "Name"),
				XMLOperating.GetNodeText(this.xmlDoc, "DuctTerminal", "AirAmount")
			};
		}

		private XmlDocument xmlDoc;

		private string filePath;

		private XmlNode xmlNodeRoot;
	}
}
